package com.css.zfzx.sjcj.modules.qhpointresultmap.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.sjcj.common.utils.CheckObjFields;
import com.css.zfzx.sjcj.common.utils.UpdateUtil;
import com.css.zfzx.sjcj.modules.qhpointresultmap.repository.PointResultMapNativeRepository;
import com.css.zfzx.sjcj.modules.qhpointresultmap.repository.PointResultMapRepository;
import com.css.zfzx.sjcj.modules.qhpointresultmap.repository.entity.PointResultMapEntity;
import com.css.zfzx.sjcj.modules.qhpointresultmap.service.PointResultMapService;
import com.css.zfzx.sjcj.modules.qhpointresultmap.viewobjects.PointResultMapQueryParams;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author zyb
 * @date 2020-12-19
 */
@Service
@Log4j
public class PointResultMapServiceImpl implements PointResultMapService {

    @Value("${file-upload.attachment-directory.config.defaultDirectory.fileDirectory}")
    private String localPath;
    @Autowired
    private PointResultMapRepository PGAResultMapRepository;
    @Autowired
    private PointResultMapNativeRepository hddcDResultmaptableNativeRepository;

    @Override
    public JSONObject queryHddcDResultmaptables(PointResultMapQueryParams queryParams, int curPage, int pageSize, String sort, String order) {
        if (!PlatformObjectUtils.isEmpty(sort) && sort.length() > 0){
            sort= CheckObjFields.propertyChange(sort);
        }
        Page<PointResultMapEntity> hddcDResultmaptablePage = this.hddcDResultmaptableNativeRepository.queryHddcDResultmaptables(queryParams, curPage, pageSize,sort,order);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcDResultmaptablePage);
        return jsonObject;
    }


    @Override
    public PointResultMapEntity getHddcDResultmaptable(String id) {
        PointResultMapEntity hddcDResultmaptable = this.PGAResultMapRepository.findById(id).orElse(null);
        return hddcDResultmaptable;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PointResultMapEntity saveHddcDResultmaptable(PointResultMapEntity hddcDResultmaptable) {
        String uuid = UUIDGenerator.getUUID();
        hddcDResultmaptable.setUuid(uuid);
        hddcDResultmaptable.setCreateUser(PlatformSessionUtils.getUserId());
        hddcDResultmaptable.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcDResultmaptable.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcDResultmaptable.setIsValid("1");
        this.PGAResultMapRepository.save(hddcDResultmaptable);
        return hddcDResultmaptable;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PointResultMapEntity updateHddcDResultmaptable(PointResultMapEntity hddcDResultmaptable) {
        PointResultMapEntity entity = PGAResultMapRepository.findById(hddcDResultmaptable.getUuid()).get();
        UpdateUtil.copyNullProperties(entity,hddcDResultmaptable);
        hddcDResultmaptable.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcDResultmaptable.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.PGAResultMapRepository.save(hddcDResultmaptable);
        return hddcDResultmaptable;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcDResultmaptables(List<String> ids) {
        List<PointResultMapEntity> hddcDResultmaptableList = this.PGAResultMapRepository.findAllById(ids);
        if (!PlatformObjectUtils.isEmpty(hddcDResultmaptableList) && hddcDResultmaptableList.size() > 0) {
            for (PointResultMapEntity hddcDResultmaptable : hddcDResultmaptableList) {
//                this.hddcDResultmaptableRepository.delete(hddcDResultmaptable);
                hddcDResultmaptable.setIsValid("0");
                hddcDResultmaptable.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcDResultmaptable.setUpdateTime(new Date());
                this.PGAResultMapRepository.save(hddcDResultmaptable);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    /**
     * 上传附件
     * @param file
     * @param filenumber
     * @return
     */
//    @Override
//    public int upload(MultipartFile file, String filenumber) {
//        List<HddcDResultmaptableEntity> hddcDResultmaptableEntity=hddcDResultmaptableRepository.findbyId(filenumber);
//        int count=0;
//        if (!hddcFileManageEntity.isEmpty()&&hddcFileManageEntity.size()!=0){
//            return count;//该编号已存在
//        }
//        try {
//            String path = localPath + "/file";//上传的路径
//            File dir = new File(path);
//            if (!dir.exists()) {
//                //路径不存在就创建路径
//                dir.mkdir();
//            }
//            String fileName = file.getOriginalFilename();
//            File file1 = new File(path, fileName);
//            file.transferTo(file1);
//            addOrUpdateHddcFileManage(fileName,filenumber);
//            //addOrUpdateHddcFileManage(fileName.substring(0,fileName.lastIndexOf('.')),filenumber);
//            count=2;//上传成功
//        } catch (IOException e) {
//            //throw new Exception(e.getMessage());
//            log.error(e.getMessage());
//            count=1;//上传失败
//        }
//        return count;
//    }
//
//    private void addOrUpdateHddcFileManage(String name,String filenumber) {
//        HddcFileManageEntity hddcFileManageEntity = new HddcFileManageEntity();
//
//        hddcFileManageEntity.setFileName(name);
//        hddcFileManageEntity.setFileId(filenumber);
//        this.saveHddcDResultmaptable(hddcFileManageEntity);
//        /*HddcFileManageQueryParams params = new HddcFileManageQueryParams();
//        params.setFileName(name);
//        params.setFileId(filenumber);
//
//        Page<HddcFileManageEntity> hddcFileManagePage = this.hddcFileManageNativeRepository.queryHddcFileManages(params, 1, 10);
//
//        HddcFileManageEntity hddcFileManageEntity = new HddcFileManageEntity();
//        hddcFileManageEntity.setFileName(name);
//        hddcFileManageEntity.setFileId(filenumber);
//
//        //update add
//        if(hddcFileManagePage.getContent().size() > 0) {
//            hddcFileManageEntity = hddcFileManagePage.getContent().get(0);
//            this.updateHddcFileManage(hddcFileManageEntity);
//        } else {
//            this.saveHddcFileManage(hddcFileManageEntity);
//        }*/
//    }

}
